//
//  MVPlayerViewController.m
//  Coco_Music
//
//  Created by dlios on 15-6-25.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "MVPlayerViewController.h"

@interface MVPlayerViewController ()

@property(nonatomic,assign)BOOL judge;

@end

@implementation MVPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    self.view.backgroundColor =[UIColor whiteColor];
    
   
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(ButtonAction:)];
    
    self.myTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.width*(9.0/16.0), self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.width*(9.0/16.0)-64) style:UITableViewStyleGrouped];
    
    self.myTableView.backgroundColor =[UIColor whiteColor];
    
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    self.myTableView.delegate =self;
    self.myTableView.dataSource =self;
    self.myView =[[UIView alloc]initWithFrame:CGRectMake(0, 0,  self.view.frame.size.width,  150)];
    self.myView.backgroundColor=[UIColor whiteColor];
    [self.myTableView addSubview:self.myView];
    [_myView release];
    
    self.myTableView.tableHeaderView = self.myView;
  
    self.titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(5, 5, self.view.frame.size.width, 30)];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font =[UIFont systemFontOfSize:20];
    self.titleLabel.numberOfLines = 0;

        [self.myView addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.DetailLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, 35, self.view.frame.size.width-10, 100)];
     

    [self.myView addSubview:self.DetailLabel];
    [_DetailLabel release];
    
    
    self.HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];

    [self getMovieData];
    [self getData];
    
  self.myArray =[NSMutableArray array];
   self.myArray1 =[NSMutableArray array];
    
    
}
-(CGFloat)heigthForCell:(NSString *)content
{
    //参数1.设置计算内容的宽和高
    //参数2.设置计算高度模式
    //参数3 设置计算字体大小属性
    //参数4.系统备用参数,设置为nil
    //其中宽度一定与现实内容的label宽度一致,否则计算不准确
    
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width-10, 10000);
    //字体大小一定与现实内容的label字体大小一致
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15.0] forKey:NSFontAttributeName];
    CGRect fram = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return fram.size.height;
}

-(void)ButtonAction:(UIBarButtonItem *)BarItem
{
    self.videoController.judge = YES;
    [self.navigationController popViewControllerAnimated:YES];
    [self.videoController dismiss];
    
}

#define MVPlayerConnect @"http://api.dongting.com/sim/mv/%ld/similarity?"
#define MVPlayerConnect1 @"app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPad4%2C4&f=f320&s=s330&imsi=&hid=&splus=8.3&active=1&net=2&openudid=5df9002635a21cc5f7f660e71a16d1e35e5a04a0&idfa=B5F4461B-F9C7-4F1E-BA92-EB2E683311DF&utdid=VYEt7JjHCIUDAGowp1W3cSW3&alf=201200&bundle_id=com.ttpod.music"

-(void)getData
{
    [self.myArray removeAllObjects];
    NSString *url =[NSString stringWithFormat:MVPlayerConnect,(long)self.MvID];
 
    
    NSString *url1 =[NSString stringWithFormat:url,MVPlayerConnect1];
    [AFN_block NSString:url1 Blokc:^(id request) {
        NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary:request];
     NSLog(@" gkdsoagna %@",url1);
        
        NSMutableArray *dataArray =[dic objectForKey:@"data"];
        for (NSMutableDictionary *myDic  in dataArray) {
            MV *mv =[[MV alloc]init];
            mv.singID =[[myDic objectForKey:@"id"]integerValue];
            mv.SingerName =[myDic objectForKey:@"singerName"];
            mv.videoName =[myDic objectForKey:@"videoName"];
            mv.desc =[myDic objectForKey:@"desc"];
           
            mv.pickCount=[[myDic objectForKey:@"pickCount"]integerValue];
            mv.stepCount =[[myDic objectForKey:@"stepCount"]integerValue];
            mv.mvList =[myDic objectForKey:@"mvList"];
            for (NSMutableDictionary *dic in mv.mvList) {
            mv.bigPicUrl=[dic objectForKey:@"picUrl"];
                mv.url =[dic objectForKey:@"url"];
            }
            [self.myArray addObject:mv];
            [mv release];
         
        }
       
        
        [self.HUD hide:YES];
        
  [self.myTableView reloadData];
    }];
}
#define MVPlayerConnect2 @"http://api.dongting.com/song/video/%ld?"
#define MVPlayerConnect3 @"app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPad4%2C4&f=f320&s=s330&imsi=&hid=&splus=8.3&active=1&net=2&openudid=5df9002635a21cc5f7f660e71a16d1e35e5a04a0&idfa=B5F4461B-F9C7-4F1E-BA92-EB2E683311DF&utdid=VYEt7JjHCIUDAGowp1W3cSW3&alf=201200&bundle_id=com.ttpod.music"
-(void)getMovieData
{
    [self.myArray1 removeAllObjects];
    
    NSString *url =[NSString stringWithFormat:MVPlayerConnect2,(long)self.MvID];
    NSString *url1 =[NSString stringWithFormat:url,MVPlayerConnect3];
   [AFN_block NSString:url1 Blokc:^(id request) {
       NSMutableDictionary *myDIc =[NSMutableDictionary dictionaryWithDictionary:request];
      
       
       NSMutableDictionary  *dataDic =[myDIc objectForKey:@"data"];
       
           MvDetail *mv =[[MvDetail alloc]init];
           mv.singerName =[dataDic objectForKey:@"singerName"];
           mv.videoName =[dataDic objectForKey:@"videoName"];
           mv.desc =[dataDic objectForKey:@"desc"];
           mv.mvList =[dataDic objectForKey:@"mvList"];
           for (NSMutableDictionary *dic in mv.mvList) {
               mv.url =[dic objectForKey:@"url"];
        self.navigationItem.title = mv.videoName;
              
           }
       NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"%@",mv.url]];
       [self addVideoPlayerWithURL:url];
    self.titleLabel.text =[NSString stringWithFormat:@"%@ --- %@", mv.videoName,mv.singerName];
    self.DetailLabel.text = [NSString stringWithFormat:@"%@",mv.desc];
   CGFloat height =[self heigthForCell:mv.desc];
     self.DetailLabel.frame = CGRectMake(10, self.titleLabel.frame.size.height+10, [UIScreen mainScreen].bounds.size.width-10, height);
       self.DetailLabel.textAlignment = NSTextAlignmentCenter;
       self.DetailLabel.numberOfLines = 0;
       self.DetailLabel.font =[UIFont systemFontOfSize:15];
       self.DetailLabel.alpha =0.8;
       NSLog(@"%f",height);
       if (height <= 150) {
  self.myView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150);
       }
       else
       {
    self.myView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height+self.titleLabel.frame.size.height+20);
       }
       self.myTableView.tableHeaderView = self.myView;
       
           
           
           [self.myArray1 addObject:mv];
           [mv release];
       
       
       [self.HUD hide:YES];
       
       
       [self.myTableView reloadData];
       
   }];

    
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{  [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MV *mv =[self.myArray objectAtIndex:indexPath.row];
//    NSURL *url = [NSURL URLWithString:mv.url];
//    [self addVideoPlayerWithURL:url];
    
    self.HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];

    self.MvID = mv.singID;
    [self getMovieData];
    [self getData];
    
    [self.myTableView reloadData];
   
    
}
//-(void)play
//{
//   NSString *str  = @"http://otmv.alicdn.com/new/mv_2_20/35/96/3563e0baf9db7034feb67e070cca0796.mp4?k=834c24359db1130f&t=1436753196";
// 
//    NSURL *url = [NSURL URLWithString:str];
//    NSLog(@"kiajohir %@",url);
//    
//    [self addVideoPlayerWithURL:url];
//}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return self.myArray.count;
}
    
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    MVPlayerCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {
        cell =[[MVPlayerCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    cell.mv =[self.myArray  objectAtIndex:indexPath.row];
    
    
    return cell;
}


- (void)addVideoPlayerWithURL:(NSURL *)url{

    if (!self.videoController) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        self.videoController = [[KrVideoPlayerController alloc] initWithFrame:CGRectMake(0, 0, width, width*(9.0/16.0))];
        
        __weak typeof(self)weakSelf = self;
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;
        }];
        [self.videoController setWillBackOrientationPortrait:^{
            [weakSelf toolbarHidden:NO];
            self.hide = YES;

        }];
        [self.videoController setWillChangeToFullscreenMode:^{
            [weakSelf toolbarHidden:YES];
            self.hide = NO;
        }];
        [self.view addSubview:self.videoController.view];
    }
    self.videoController.contentURL = url;
     [self.HUD hide:YES];
    
}
//隐藏navigation tabbar 电池栏
- (void)toolbarHidden:(BOOL)Bool
{
    
    [self.navigationController setNavigationBarHidden:Bool animated:YES];
    self.tabBarController.tabBar.hidden = Bool;
    [[UIApplication sharedApplication] setStatusBarHidden:TRUE];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
    //UIStatusBarStyleDefault = 0 黑色文字，浅色背景时使用
    //UIStatusBarStyleLightContent = 1 白色文字，深色背景时使用
}

- (BOOL)prefersStatusBarHidden
{
    
    return self.hide; // 返回NO表示要显示，返回YES将hiden
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
